package com.itbaizhan.service.impl;

import com.itbaizhan.dao.BannerDao;
import com.itbaizhan.exception.BusException;
import com.itbaizhan.pojo.Banner;
import com.itbaizhan.result.CodeEnum;
import com.itbaizhan.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author spf
 * @date 2024/3/4
 * @time 9:07
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerDao bannerDao;
    @Value("${livegoods.banner.nginx.prefix}")
    private String nginxPrefix;

    @Override
    public List<Banner> getBanners() {
        try {
            Query query = new Query();
            query.with(PageRequest.of(0,4));
            List<Banner> banners = bannerDao.selectBanners(query);

            // 遍历集合修改图片地址
            for (Banner banner : banners) {
                banner.setUrl(nginxPrefix + banner.getUrl());
            }

            return banners;
        } catch (Exception e) {
            e.printStackTrace();
            // 失败
            throw new BusException(CodeEnum.FIND_BANNER_ERROR);
        }
    }
}
